package com.snow.useviewdemo.viewpager2;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.snow.common.base.BaseBasesActivity;
import com.snow.useviewdemo.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhaoyingtao
 * Date: 2020/6/18
 * Describe:
 */
public class NewsViewPager2Activity extends BaseBasesActivity {
    @BindView(R.id.tablayout)
    TabLayout mTabLayout;
    @BindView(R.id.viewpager2)
    ViewPager2 mViewPager2;
    ViewPager2FragmentAdapter fragmentAdapter;

    //自定义指示条参考
    // https://blog.csdn.net/u013719138/article/details/89964674
    @Override
    protected int getContentViewId() {
        return R.layout.activity_news_viewpager2;
    }

    @Override
    protected void initView() {
        //默认不预加载，默认值是-1 不能设置0，要设置预加载页面只需设置大于0的整数即可
        mViewPager2.setOffscreenPageLimit(1);

        mTabLayout.addTab(mTabLayout.newTab().setText("Tab0"));
        mTabLayout.addTab(mTabLayout.newTab().setText("Tab1"));
        mTabLayout.addTab(mTabLayout.newTab().setText("Tab2"));
        mTabLayout.addTab(mTabLayout.newTab().setText("Tab3"));


        initData();
        initListener();
    }

    private void initData() {
        ArrayList<Fragment> list = new ArrayList<>();
        list.add(NewsFragment.getInstance("你哈"));
        list.add(NewsFragment.getInstance("记号笔"));
        list.add(NewsFragment.getInstance("受到广泛"));
        list.add(NewsFragment.getInstance("地方"));

        fragmentAdapter = new ViewPager2FragmentAdapter(this, list);
        mViewPager2.setAdapter(fragmentAdapter);
    }

    private void initListener() {
        //TabLayout和ViewPager的绑定 ====需要依赖 implementation 'com.google.android.material:material:1.3.0-alpha01'
        new TabLayoutMediator(mTabLayout, mViewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText("tab" + position);
            }
        }
        ).attach();
    }

}
